package common;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;


import com.example.xuegengwang.xuegengwang.R;

import org.xutils.x;


import java.util.HashMap;

import guidePage.LoginPage;
import mainFragment.activity.CollegeIndex;
import utils.VariousUtils;
import web.activity.WebPage;
import utils.KeyBoard;
import utils.MyLogUtils;
import utils.SPUtils;
import utils.StatusBarCompatUtils;
import utils.ToastUtils;

/**
 * Created by admin on 2017/2/13.
 * 基类
 */

public class BaseActivity extends FragmentActivity {
    private static final String TAG = "BaseActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//禁止横屏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        StatusBarCompatUtils.compat(this, R.color.common_color);
        MyLogUtils.e(TAG, "onCreate: " + getClass().getName());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void drumpActivity(String url) {
        //跳转activity用的
        Intent intent = new Intent();
        if (url.contains("http")) {
            //跳转到新闻页
            String url1 = VariousUtils.noHttps(url);
            intent.putExtra("url", url1);
            String[] split = url1.split("[?]");
            if(split!=null){
                if(split.length>1){
                    String newsid = split[1];
                    String[] split1 = newsid.split("=");
                    if(split1!=null){
                        if(split1.length>1){
                            String newsid1 = split1[1];
                            intent.putExtra("newsid", newsid1);
                        }
                    }
                }
            }
            intent.setClass(this, WebPage.class);
            startActivity(intent);
            return;
        }
        if (url.contains("jz:")) {
            //跳转页面
            String action = url.split("//")[1];
            if (action.contains("::")) {
                //有参数
                String action1 = action.split("::")[0];
                intent.setAction(action1);
                String params = action.split("::")[1];
                if (params.contains("&")) {
                    //有多个参数
                    String[] split = params.split("&");
                    HashMap<String, String> hashMap = new HashMap<>();
                    for (int i = 0; i < split.length; i++) {
                        String[] split1 = split[i].split("=");
                        if (split1.length > 1) {
                            //有key value的情况
                            String key = split1[0];//key
                            String value = split1[1];//value
                            hashMap.put(key, value);
                        }
                    }
                    intent.putExtra("params", hashMap);
                    startActivity(intent);
                } else {
                    //只有一个参数
                    HashMap<String, String> hashMap = new HashMap<>();
                    String[] split = params.split("=");
                    if (split.length > 1) {
                        //有key value的情况
                        String key = split[0];//key
                        String value = split[1];//value
                        hashMap.put(key, value);
                    }
                    intent.putExtra("params", hashMap);
                    startActivity(intent);
                }
            } else {
                //跳到对应的action的activity,没参数
                intent.setAction(action);
                startActivity(intent);
                return;
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                IBinder windowToken = getCurrentFocus().getWindowToken();
                KeyBoard.hideKeyBoard(windowToken);
            }
        }
        return super.onTouchEvent(event);
    }

    public boolean hasLoad() {
        String string = SPUtils.getSharedPreferences().getString(Constant.USER_ID, "");
        if (TextUtils.isEmpty(string)) {
            return false;
        } else {
            return true;
        }
    }
    public void login(Context context) {
        ToastUtils.shortToast("您尚未登录，登录后可以使用此功能");
        Intent i = new Intent();
        i.setClass(context, LoginPage.class);
        ((Activity) context).startActivityForResult(i, Constant.LOGIN_REQUEST_CODE);
        ((Activity) context).overridePendingTransition(R.anim.zoomin, 0);
    }
}
